package ysq.xyg.web.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.web.dao.WebRecordDao;
import ysq.xyg.web.model.po.WebRecord;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebRecordVo;
import ysq.xyg.web.service.WebRecordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-交易记录service实现
 *
 * @author shengQiang yu
 * @date 2019-4-26 13:44:31
 */
@Service("webRecordService")
public class WebRecordServiceImpl extends BaseServiceImpl<WebRecord> implements WebRecordService {

    @Resource
    private WebRecordDao webRecordDao;

    private WebRecord webRecord;

    @Autowired
    public void setBaseDao(WebRecordDao webRecordDao) {
        this.baseDao = webRecordDao;
    }

    /**
     * 左关联分页查询
     *
     * @param webRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 13:44:31
     */
    public ReturnEntity pageLeft(WebRecordVo webRecordVo, int pageSize, int pageNum) {
        if(StrUtil.isNotEmpty(webRecordVo.getNickName())){
            webRecordVo.setNickName(BaseUtil.encodeBase64String(webRecordVo.getNickName()));
        }
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebRecord> list = webRecordDao.leftList(webRecordVo);
        if(list!=null&&list.size()>0){
            for(WebRecord webRecord:list){
                if(StrUtil.isNotEmpty(webRecord.getNickName())){
                    webRecord.setNickName(BaseUtil.decodeBase64(webRecord.getNickName()));
                }
            }
        }
        PageInfo<WebRecord> page=new PageInfo<WebRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 分页查询
     *
     * @param webRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 13:44:31
     */
    public ReturnEntity page(WebRecordVo webRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebRecord> list = acceptList(webRecordVo);
        PageInfo<WebRecord> page=new PageInfo<WebRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webRecordVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 13:44:31
     */
    public ReturnEntity addOrUpdate(WebRecordVo webRecordVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webRecordVo.getId())){
            webRecord=new WebRecord(currentUser);
            isUpate=false;
        }else{
            webRecord=selectByPrimaryKey(webRecordVo.getId());
            if(ObjectUtil.isNull(webRecord)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webRecord.setUpdateTime(new Date());
            webRecord.setUpdateUser(currentUser.getId());
        }
        webRecord.setMemberId(webRecordVo.getMemberId());
        webRecord.setMoney(webRecordVo.getMoney());
        webRecord.setType(webRecordVo.getType());
        webRecord.setExtraId(webRecordVo.getExtraId());
        webRecord.setDescripts(webRecordVo.getDescripts());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webRecord);
        }else{
            num=insert(webRecord);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-26 13:44:31
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebRecord webRecord=selectByPrimaryKey(id);
        if(webRecord!=null){
            webRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webRecord.setUpdateTime(new Date());
            webRecord.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webRecord);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webRecordVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-26 13:44:31
     */
    public List<WebRecord> acceptList(WebRecordVo webRecordVo){
        Example example = new Example(WebRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webRecordVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webRecordVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webRecordDao.selectByExample(example);
    }
    /**
     * 数据导出
     *
     * @param webRecordVo 对象
     * @author shengQiang yu
     * @date 2019-4-26 13:44:31
     */
    @Override
    public void export(HttpServletResponse response, WebRecordVo webRecordVo) {
        String title="交易记录";
        List<WebRecord> list=webRecordDao.leftList(webRecordVo);
        title=title+"_"+ DateUtil.format(new Date(),"yyyyMMdd");
        // 导出操作
        EasyPoiUtil.exportExcel(list, title, "交易信息", WebRecord.class, title+".xls", response);
    }
}
